package com.coldchain.cys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.coldchain.common.msg.BaseResponse;
import com.coldchain.common.msg.ObjectRestResponse;
import com.coldchain.common.util.MD5Utils;
import com.coldchain.cys.constant.Constant;
import com.coldchain.cys.context.CysContext;
import com.coldchain.cys.entity.Cys;
import com.coldchain.cys.entity.CysChild;
import com.coldchain.cys.mapper.CysChildMapper;
import com.coldchain.cys.service.ICysChildService;
import com.coldchain.cys.service.ICysService;
import com.fangliai.core.context.BaseContextHandler;
import com.fangliai.core.entitiy.SystemModelEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianpeng
 * @since 2018-09-11
 */
@Service
public class CysChildServiceImpl extends ServiceImpl<CysChildMapper, CysChild> implements ICysChildService {

	@Autowired
	private ICysService cysService ;
	
	@Override
	public CysChild getByPhone(String phone) {
		QueryWrapper<CysChild> ew = new QueryWrapper<CysChild>();
		ew.eq("phone", phone) ;
		return super.getOne(ew) ;
	}

	@Override
	public String validateCysChild(Long cysChildId) {
		CysChild cysChild = super.getById(cysChildId);
		if(cysChild == null){
			return "当前承运商不存在" ;
		}
		if(cysChild.getIsdel() == Constant.LOGIC_DELETE){
			return "当前子账号已被删除" ;
		}
		if(cysChild.getIsuse() == Constant.DISABLE_USE){
			return "当前账号是禁用状态" ;
		}
		Cys cys = cysService.getById(cysChild.getCid()) ;
		if(cys == null){
			return "登录异常" ;
		}
		if(cys.getIsdel() == Constant.LOGIC_DELETE){
			return "当前承运商已被删除" ;
		}
		if(cys.getIsuse() == Constant.DISABLE_USE){
			return "当前承运商是禁用状态" ;
		}
		return null;
	}

	@Override
	public List<CysChild> getCurrentCys() {
		Long cysId = CysContext.getCysID();
		QueryWrapper<CysChild> ew = new QueryWrapper<CysChild>();
		ew.eq("cid", cysId) ;
		List<CysChild> data = super.list(ew);
		return data ; 
	}

	@Transactional
	@Override
	public BaseResponse doSwitchCysChildUse(Long cysChildId) {
		Long cysId = CysContext.getCysID();
		CysChild cysChild = super.getById(cysChildId);
		String sysModel = BaseContextHandler.getSysModl();
		if(sysModel.equals(SystemModelEnum.USER.name()) || sysModel.equals(SystemModelEnum.DRIVER.name())) {
			return getErrorMessage("无权操作") ;
		}
		if(sysModel.equals(SystemModelEnum.CYS.name())) {
			if(!cysChild.getCid().equals(cysId)) {
				return getErrorMessage("无权操作") ;
			}
		}
		if(cysChild == null) {
			return getErrorMessage("子账号不存在") ;
		}
		if(cysChild.getIsuse() == Constant.ENABLE_USE) {
			cysChild.setIsuse(Constant.DISABLE_USE);
		}else {
			cysChild.setIsuse(Constant.ENABLE_USE);
		}
		boolean result = super.updateById(cysChild);
		if(!result) {
			return getErrorMessage("操作异常") ;
		}
		return new ObjectRestResponse<>().succeed("操作成功") ;
	}
	
	private BaseResponse getErrorMessage(String message){
		return new ObjectRestResponse<>().error(message) ;
	}

	@Transactional
	@Override
	public BaseResponse doAddOrEditCysChild(CysChild cysChild) {
		Long cysId = CysContext.getCysID(); //承运商的id
		String sysModel = BaseContextHandler.getSysModl() ; 
		if(sysModel.equals(SystemModelEnum.USER.name()) || sysModel.equals(SystemModelEnum.DRIVER.name())) {
			return getErrorMessage("无权操作") ;
		}
		if(StrUtil.isBlank(cysChild.getPhone())) {
			return getErrorMessage("承运商子账号名称不能为空") ;
		}
		if(cysChild.getPhone().length() != 11) {
			return getErrorMessage("承运商子账号必须是11位的电话号码") ;
		}
		if(StrUtil.isBlank(cysChild.getPassword())) {
			return getErrorMessage("承运商子账号的登录密码不能为空") ;
		}
		if(cysChild.getRoleid() == null) {
			return getErrorMessage("必须指定一个岗位") ;
		}
		if(cysChild.getId() != null) {
			//修改操作
			cysChild.setPhone(null); //不能更改手机号码
			CysChild original = super.getById(cysChild); //原始的子账号
			if(sysModel.equals(SystemModelEnum.CYS.name())) {
				if(!original.getCid().equals(cysId)) {
					return getErrorMessage("无权操作") ;
				}
			}
		}else {
			//否则就是添加
			Cys cys = cysService.getByPhone(cysChild.getPhone());
			CysChild cc = this.getByPhone(cysChild.getPhone()) ;
			if(cys != null || cc != null) {
				//只要有一个不等于空
				return getErrorMessage("该号码已被注册") ;
			}
		}
		cysChild.setCid(cysId); 
		String encodingPass = MD5Utils.getMD5Str(cysChild.getPassword());
		cysChild.setPassword(encodingPass);
		boolean result = super.saveOrUpdate(cysChild);
		if(!result) {
			return getErrorMessage("操作失败") ;
		}
		return new ObjectRestResponse<>().succeed("操作成功") ;
	}
	
}
